﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Model;
using System.Collections.Generic;
namespace DAL
{
    /// <summary>
    /// 数据访问类:PMS_ProjectMaintain
    /// </summary>
    public class PMS_ProjectMaintainDAL
    {
        public PMS_ProjectMaintainDAL()
        { }

        /// <summary>
        /// 根据项目编号查询
        /// </summary>
        /// <param name="projectID"></param>
        /// <returns></returns>
        public List<PMS_ProjectMaintain> GetList(PMS_ProjectMaintainQuery query)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"select a.*,b.city from PMS_ProjectMaintain as a
left join PMS_Project as b on a.projectID=b.id
where a.isDelete='0' and a.projectID=@projectID ");
            if (query.startDate != null && query.startDate != DateTime.MinValue)
            {
                strSql.Append(" and a.signingDate>='" + query.startDate + "' ");
            }
            if (query.endDate != null && query.endDate != DateTime.MinValue)
            {
                strSql.Append(" and a.signingDate<='" + query.endDate + "' ");
            }
            strSql.Append(" order by a.createDate desc");
            SqlParameter[] parameters = { new SqlParameter("@projectID", query.projectID) };
            return SqlHelper.GetDataByColumnsLst<PMS_ProjectMaintain>(Comm.Config.PMS, strSql.ToString(), parameters);
        }

        /// <summary>
        /// 根据项目编号查询
        /// </summary>
        /// <param name="projectID"></param>
        /// <returns></returns>
        public DataTable GetListTable(PMS_ProjectMaintainQuery query)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"select projectIDS=b.projectID,b.projectName,b.city,a.* from PMS_ProjectMaintain as a
left join PMS_Project as b on a.projectID=b.id
where a.isDelete='0' and a.projectID=@projectID ");
            if (query.startDate != null && query.startDate != DateTime.MinValue)
            {
                strSql.Append(" and a.signingDate>='" + query.startDate + "' ");
            }
            if (query.endDate != null && query.endDate != DateTime.MinValue)
            {
                strSql.Append(" and a.signingDate<='" + query.endDate + "' ");
            }
            strSql.Append(" order by a.createDate desc");
            SqlParameter[] parameters = { new SqlParameter("@projectID", query.projectID) };
            return SqlHelper.ExecuteGetTable(Comm.Config.PMS, CommandType.Text, strSql.ToString(), parameters);
        }

        /// <summary>
        /// 根据项目编号查询
        /// </summary>
        /// <param name="projectID"></param>
        /// <returns></returns>
        public List<PMS_ProjectMaintain> GetList(string projectID)
        {
            string sql = @"select a.*,b.city from PMS_ProjectMaintain as a
left join PMS_Project as b on a.projectID=b.id
where a.isDelete='0' and a.projectID=@projectID order by a.createDate desc";
            SqlParameter[] parameters = { new SqlParameter("@projectID", projectID) };
            return SqlHelper.GetDataByColumnsLst<PMS_ProjectMaintain>(Comm.Config.PMS, sql, parameters);
        }

        /// <summary>
        /// 单条
        /// </summary>
        /// <param name="projectID"></param>
        /// <returns></returns>
        public PMS_ProjectMaintain GetModel(int id)
        {
            string sql = @"select a.*,b.city from PMS_ProjectMaintain as a
left join PMS_Project as b on a.projectID=b.id
where a.isDelete='0' and a.id=@id";
            SqlParameter[] parameters = { new SqlParameter("@id", id) };
            return SqlHelper.GetDataByColumns<PMS_ProjectMaintain>(Comm.Config.PMS, sql, parameters);
        }

        #region  Method
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Model.PMS_ProjectMaintain model)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@projectID", SqlDbType.NVarChar,50),
					new SqlParameter("@bankName", SqlDbType.NVarChar,50),
					new SqlParameter("@quarter", SqlDbType.NVarChar,50),
					new SqlParameter("@planDate", SqlDbType.DateTime),
					new SqlParameter("@completeDate", SqlDbType.DateTime),
					new SqlParameter("@description", SqlDbType.NVarChar,3000),
					new SqlParameter("@attachment", SqlDbType.NVarChar,1000)};

            parameters[0].Value = model.projectID == null ? "" : model.projectID;
            parameters[1].Value = model.bankName == null ? "" : model.bankName;
            parameters[2].Value = model.quarter == null ? "" : model.quarter;
            if (model.planDate == null)
            {
                parameters[3].Value = DBNull.Value;
            }
            else
            {
                parameters[3].Value = model.planDate;
            }
            if (model.completeDate == null)
            {
                parameters[4].Value = DBNull.Value;
            }
            else
            {
                parameters[4].Value = model.completeDate;
            }
            parameters[5].Value = model.description == null ? "" : model.description;
            parameters[6].Value = model.attachment == null ? "" : Comm.Config.UploadPath + "Maintain/" + model.attachment;

            int rows = SqlHelper.ExecuteNonQuery(Comm.Config.PMS, CommandType.StoredProcedure, "sp_AddProjectMaintain", parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.PMS_ProjectMaintain model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update PMS_ProjectMaintain set ");
            strSql.Append("bankName=@bankName,");
            strSql.Append("quarter=@quarter,");
            strSql.Append("planDate=@planDate,");
            strSql.Append("completeDate=@completeDate,");
            strSql.Append("description=@description,");
            strSql.Append("attachment=@attachment");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4),
					new SqlParameter("@bankName", SqlDbType.NVarChar,50),
					new SqlParameter("@quarter", SqlDbType.NVarChar,50),
					new SqlParameter("@planDate", SqlDbType.DateTime),
					new SqlParameter("@completeDate", SqlDbType.DateTime),
					new SqlParameter("@description", SqlDbType.NVarChar,3000),
					new SqlParameter("@attachment", SqlDbType.NVarChar,1000)};
            parameters[0].Value = model.id;
            parameters[1].Value = model.bankName == null ? "" : model.bankName;
            parameters[2].Value = model.quarter == null ? "" : model.quarter;
            if (model.planDate == null)
            {
                parameters[3].Value = DBNull.Value;
            }
            else
            {
                parameters[3].Value = model.planDate;
            }
            if (model.completeDate == null)
            {
                parameters[4].Value = DBNull.Value;
            }
            else
            {
                parameters[4].Value = model.completeDate;
            }
            parameters[5].Value = model.description == null ? "" : model.description;
            if (string.IsNullOrEmpty(model.attachment))
            {
                parameters[6].Value = DBNull.Value;
            }
            else
            {
                parameters[6].Value = Comm.Config.UploadPath + "Maintain/" + model.attachment;
            }
            int rows = SqlHelper.ExecuteNonQuery(Comm.Config.PMS, CommandType.Text, strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {
            //该表无主键信息，请自定义主键/条件字段
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update PMS_ProjectMaintain set isDelete='1' ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = { new SqlParameter("@id", id) };

            int rows = SqlHelper.ExecuteNonQuery(Comm.Config.PMS, CommandType.Text, strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        #endregion  Method
    }
}

